package com.atsmart.app.activity;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;

import com.atsmart.app.R;
import com.atsmart.app.R.drawable;
import com.atsmart.app.utils.PersistentKeyUtil;
import com.atsmart.app.utils.PrefsUtils;
import com.atsmart.app.utils.StringUtil;

public class NavActivity extends TabActivity {
	TabHost tabHost;
	GestureDetector mGestureDetector;
	private RelativeLayout tab1;
	private RelativeLayout tab2;
	private RelativeLayout tab3;
	private RelativeLayout tab4;
	private RelativeLayout tab5;
	private RelativeLayout tab6;
	private Button tabTask;
	private Button tabReport;
	private Button tabClient;
	private Button tabMessage;
	private Button tabAction;
	private Button tabProduct;
	private TextView txtTask;
	private TextView txtReport;
	private TextView txtClient;
	private TextView txtMsg;
	private TextView txtProduct;
	private TextView txtAction;
	private String[] strings = {};
	private ImageView imageView;
	public static NavActivity instance = null; 
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.nav_tabs);
		
		instance = this;
		
		// mGestureDetector = new GestureDetector(this);
		LinearLayout ll = (LinearLayout) findViewById(R.id.linew);
		// ll.setOnTouchListener(this);

		Bundle bundle = getIntent().getExtras();
		String string = "";
		if(bundle != null){
			string = bundle.getString("updata_info");
		}
		try{
			if(!StringUtil.isEmptyOrNull(string)){
				strings = string.split(",");
			}else{
				strings = null;
			}
		}catch (Exception e) {
		}
		ll.setLongClickable(true);
		tabHost = getTabHost();
		tabHost.setup();
		Intent intent1 = new Intent(this, TaskListActivity.class);
		createTab("任务", intent1);

		Intent intent2 = new Intent(this, ReportListActivity.class);
		createTab("汇报", intent2);

		Intent intent3 = new Intent(this, ClientListActivity.class);
		createTab("客户", intent3);

		Intent intent4 = new Intent(this, ProductListActivity.class);
		createTab("产品", intent4);

		Intent intent5 = new Intent(this, MessageListActivity.class);
		createTab("消息", intent5);
		
		Intent intent6 = new Intent(this, OtherListActivity.class);
		createTab("其他", intent6);

		TabWidget tabWidget = tabHost.getTabWidget();
		int count = tabWidget.getChildCount();

		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		//
		// 窗口的宽度
		int screenWidth = dm.widthPixels;
		if (count > 5) {
			for (int i = 0; i < count; i++) {
				tabWidget.getChildTabViewAt(i).setMinimumWidth(screenWidth / 5);
			}
		}
		
		try{
			tabHost.setCurrentTab(PrefsUtils.getSubTab());
		}catch (Exception e) {
			PrefsUtils.setSubTab(0);
			tabHost.setCurrentTab(0);
		}
		
		tab1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				selectTab1();
			}
		});
		tab2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				selectTab2();
			}
		});
		tab3.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				selectTab3();
			}
		});
		tab4.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				selectTab4();
			}
		});
		tab5.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				selectTab5();
			}
		});
		tab6.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				selectTab6();
			}
		});
		
		if(!StringUtil.isEmptyOrNull(PrefsUtils.getNotification())){
//			Log.e("TAG", "notify: = " + PrefsUtils.getNotification());
			PrefsUtils.setNotifiction(null);
			selectTab5();
		}
	}

	private void createTab(String text, Intent intent) {
		tabHost.addTab(tabHost.newTabSpec(text)
				.setIndicator(createTabView(text)).setContent(intent));
	}

	private View createTabView(String text) {
		View view = LayoutInflater.from(this).inflate(R.layout.tab_indicator,
				null);
		if ("任务".equals(text)) {
			tab1 = (RelativeLayout) view.findViewById(R.id.tab211);
			tabTask = (Button) view.findViewById(R.id.imageView1);
			tabTask.setBackgroundResource(drawable.tab_task);
			txtTask = (TextView) view.findViewById(R.id.tv_tab);
			imageView = (ImageView) view.findViewById(R.id.updata_image);
			txtTask.setText(text);
			if(strings != null && strings.length >= 1){
				if(Integer.parseInt(strings[0]) == 1){
					imageView.setVisibility(View.VISIBLE);
				}else{
					imageView.setVisibility(View.GONE);
				}
			}
			// tab1.setOnClickListener(this);
		}
		if ("汇报".equals(text)) {
			imageView = (ImageView) view.findViewById(R.id.updata_image);
			tab2 = (RelativeLayout) view.findViewById(R.id.tab211);
			tabReport = (Button) view.findViewById(R.id.imageView1);
			tabReport.setBackgroundResource(drawable.tab_report);
			txtReport = (TextView) view.findViewById(R.id.tv_tab);
			txtReport.setText(text);
			if(strings != null && strings.length >= 2){
				if(Integer.parseInt(strings[1]) == 1){
					imageView.setVisibility(View.VISIBLE);
				}else{
					imageView.setVisibility(View.GONE);
				}
			}
			// tab2.setOnClickListener(this);
		}
		if ("客户".equals(text)) {
			imageView = (ImageView) view.findViewById(R.id.updata_image);
			tab3 = (RelativeLayout) view.findViewById(R.id.tab211);
			tabClient = (Button) view.findViewById(R.id.imageView1);
			tabClient.setBackgroundResource(drawable.tab_client);
			txtClient = (TextView) view.findViewById(R.id.tv_tab);
			txtClient.setText(text);
			if(strings != null && strings.length >= 3){
				if(Integer.parseInt(strings[2]) == 1){
					imageView.setVisibility(View.VISIBLE);
				}else{
					imageView.setVisibility(View.GONE);
				}
			}
			// tab3.setOnClickListener(this);
		}
		if ("产品".equals(text)) {
			imageView = (ImageView) view.findViewById(R.id.updata_image);
			tab4 = (RelativeLayout) view.findViewById(R.id.tab211);
			tabProduct = (Button) view.findViewById(R.id.imageView1);
			tabProduct.setBackgroundResource(drawable.tab_product);
			txtProduct = (TextView) view.findViewById(R.id.tv_tab);
			txtProduct.setText(text);
			if(strings != null && strings.length >= 4){
				if(Integer.parseInt(strings[3]) == 1){
					imageView.setVisibility(View.VISIBLE);
				}else{
					imageView.setVisibility(View.GONE);
				}
			}
			// tab4.setOnClickListener(this);
		}

		if ("消息".equals(text)) {
			imageView = (ImageView) view.findViewById(R.id.updata_image);
			tab5 = (RelativeLayout) view.findViewById(R.id.tab211);
			tabMessage = (Button) view.findViewById(R.id.imageView1);
			tabMessage.setBackgroundResource(drawable.tab_message);
			txtMsg = (TextView) view.findViewById(R.id.tv_tab);
			txtMsg.setText(text);
			if(strings != null && strings.length >= 5){
				if(Integer.parseInt(strings[4]) == 1){
					imageView.setVisibility(View.VISIBLE);
				}else{
					imageView.setVisibility(View.GONE);
				}
			}
			// tab5.setOnClickListener(this);
		}
		if ("其他".equals(text)) {
			imageView = (ImageView) view.findViewById(R.id.updata_image);
			tab6 = (RelativeLayout) view.findViewById(R.id.tab211);
			tabAction = (Button) view.findViewById(R.id.imageView1);
			tabAction.setBackgroundResource(drawable.tab_action_selector);
			txtAction = (TextView) view.findViewById(R.id.tv_tab);
			txtAction.setText(text);
			// tab6.setOnClickListener(this);
		}
		return view;
	}

	private void selectTab() {
//		int activeTab = BizApp.getInstance().getTab();
		int activeTab = PrefsUtils.getSubTab();
			
		 Log.e("当前进入", "tab: = " + String.valueOf(activeTab));
		if (tabTask != null && activeTab == PersistentKeyUtil.TAB_TASK) {
			selectTab1();
		} else if (tabReport != null && activeTab == PersistentKeyUtil.TAB_REPORT) {
			selectTab2();
		} else if (tabClient != null && activeTab == PersistentKeyUtil.TAB_CLIENT) {
			selectTab3();
		} else if (tabMessage != null && activeTab == PersistentKeyUtil.TAB_MESSAGE) {
			selectTab4();
		} else if (tabAction != null && activeTab == PersistentKeyUtil.TAB_ACTION) {
			selectTab5();
		} else if (tabProduct != null && activeTab == PersistentKeyUtil.TAB_PRODUCT) {
			selectTab6();
		} else {
			selectTab1();
		}
//		boolean isNoti = false;
//		if(!StringUtil.isEmptyOrNull(PrefsUtils.getIsNoti())){
////			isNoti = true;
//			PrefsUtils.setIsNoti(null);
//		}
		
	}

	private void selectTab1() {
		PrefsUtils.setIsNoti(null);
		tab1.setBackgroundResource(R.drawable.tab_bg_press);
		tab2.setBackgroundResource(0);
		tab3.setBackgroundResource(0);
		tab4.setBackgroundResource(0);
		tab5.setBackgroundResource(0);
		tab6.setBackgroundResource(0);
		tabTask.setBackgroundResource(R.drawable.tab_task_press);
		tabReport.setBackgroundResource(R.drawable.tab_report);
		tabClient.setBackgroundResource(R.drawable.tab_client);
		tabMessage.setBackgroundResource(R.drawable.tab_message);
		tabAction.setBackgroundResource(R.drawable.tab_dynamic);
		txtTask.setTextColor(getResources().getColor(R.color.deap_blue_text));
		txtReport.setTextColor(getResources().getColor(R.color.dark_text));
		txtClient.setTextColor(getResources().getColor(R.color.dark_text));
		txtMsg.setTextColor(getResources().getColor(R.color.dark_text));
		txtAction.setTextColor(getResources().getColor(R.color.dark_text));
		tabProduct.setBackgroundResource(R.drawable.tab_product);
		txtProduct.setTextColor(getResources().getColor(R.color.dark_text));
//		BizApp.getInstance().setTab(PersistentKeyUtil.TAB_TASK);
		PrefsUtils.setSubTab(PersistentKeyUtil.TAB_TASK);
		tabHost.setCurrentTab(0);
	}

	private void selectTab2() {
		PrefsUtils.setIsNoti(null);
		tab1.setBackgroundResource(0);
		tab2.setBackgroundResource(R.drawable.tab_bg_press);
		tab3.setBackgroundResource(0);
		tab4.setBackgroundResource(0);
		tab5.setBackgroundResource(0);
		tab6.setBackgroundResource(0);
		tabTask.setBackgroundResource(R.drawable.tab_task);
		tabReport.setBackgroundResource(R.drawable.tab_report_press);
		tabClient.setBackgroundResource(R.drawable.tab_client);
		tabMessage.setBackgroundResource(R.drawable.tab_message);
		tabAction.setBackgroundResource(R.drawable.tab_dynamic);
		txtTask.setTextColor(getResources().getColor(R.color.dark_text));
		txtReport.setTextColor(getResources().getColor(R.color.deap_blue_text));
		txtClient.setTextColor(getResources().getColor(R.color.dark_text));
		txtMsg.setTextColor(getResources().getColor(R.color.dark_text));
		txtAction.setTextColor(getResources().getColor(R.color.dark_text));
		tabProduct.setBackgroundResource(R.drawable.tab_product);
		txtProduct.setTextColor(getResources().getColor(R.color.dark_text));
//		BizApp.getInstance().setTab(PersistentKeyUtil.TAB_REPORT);
		PrefsUtils.setSubTab(PersistentKeyUtil.TAB_REPORT);
		tabHost.setCurrentTab(1);
	}

	private void selectTab3() {
		PrefsUtils.setIsNoti(null);
		tab1.setBackgroundResource(0);
		tab2.setBackgroundResource(0);
		tab3.setBackgroundResource(R.drawable.tab_bg_press);
		tab4.setBackgroundResource(0);
		tab5.setBackgroundResource(0);
		tab6.setBackgroundResource(0);
		tabTask.setBackgroundResource(R.drawable.tab_task);
		tabReport.setBackgroundResource(R.drawable.tab_report);
		tabClient.setBackgroundResource(R.drawable.tab_client_press);
		tabMessage.setBackgroundResource(R.drawable.tab_message);
		tabAction.setBackgroundResource(R.drawable.tab_dynamic);
		txtTask.setTextColor(getResources().getColor(R.color.dark_text));
		txtReport.setTextColor(getResources().getColor(R.color.dark_text));
		txtClient.setTextColor(getResources().getColor(R.color.deap_blue_text));
		txtMsg.setTextColor(getResources().getColor(R.color.dark_text));
		txtAction.setTextColor(getResources().getColor(R.color.dark_text));
		tabProduct.setBackgroundResource(R.drawable.tab_product);
		txtProduct.setTextColor(getResources().getColor(R.color.dark_text));
//		BizApp.getInstance().setTab(PersistentKeyUtil.TAB_CLIENT);
		PrefsUtils.setSubTab(PersistentKeyUtil.TAB_CLIENT);
		tabHost.setCurrentTab(2);
	}

	private void selectTab4() {
		PrefsUtils.setIsNoti(null);
		tab1.setBackgroundResource(0);
		tab2.setBackgroundResource(0);
		tab3.setBackgroundResource(0);
		tab4.setBackgroundResource(R.drawable.tab_bg_press);
		tab5.setBackgroundResource(0);
		tab6.setBackgroundResource(0);
		tabTask.setBackgroundResource(R.drawable.tab_task);
		tabReport.setBackgroundResource(R.drawable.tab_report);
		tabClient.setBackgroundResource(R.drawable.tab_client);
		tabMessage.setBackgroundResource(R.drawable.tab_message);
		tabAction.setBackgroundResource(R.drawable.tab_dynamic);
		txtTask.setTextColor(getResources().getColor(R.color.dark_text));
		txtReport.setTextColor(getResources().getColor(R.color.dark_text));
		txtClient.setTextColor(getResources().getColor(R.color.dark_text));
		txtMsg.setTextColor(getResources().getColor(R.color.dark_text));
		txtAction.setTextColor(getResources().getColor(R.color.dark_text));
		tabProduct.setBackgroundResource(R.drawable.tab_product_press);
		txtProduct
				.setTextColor(getResources().getColor(R.color.deap_blue_text));
//		BizApp.getInstance().setTab(PersistentKeyUtil.TAB_MESSAGE);
		PrefsUtils.setSubTab(PersistentKeyUtil.TAB_MESSAGE);
		tabHost.setCurrentTab(3);
	}

	private void selectTab5() {
		PrefsUtils.setIsNoti(null);
		tab1.setBackgroundResource(0);
		tab2.setBackgroundResource(0);
		tab3.setBackgroundResource(0);
		tab4.setBackgroundResource(0);
		tab6.setBackgroundResource(0);
		tab5.setBackgroundResource(R.drawable.tab_bg_press);
		tabTask.setBackgroundResource(R.drawable.tab_task);
		tabReport.setBackgroundResource(R.drawable.tab_report);
		tabClient.setBackgroundResource(R.drawable.tab_client);
		tabMessage.setBackgroundResource(R.drawable.tab_message_press);
		tabAction.setBackgroundResource(R.drawable.tab_dynamic);
		tabProduct.setBackgroundResource(R.drawable.tab_product);
		txtTask.setTextColor(getResources().getColor(R.color.dark_text));
		txtReport.setTextColor(getResources().getColor(R.color.dark_text));
		txtClient.setTextColor(getResources().getColor(R.color.dark_text));
		txtMsg.setTextColor(getResources().getColor(R.color.deap_blue_text));
		txtAction.setTextColor(getResources().getColor(R.color.dark_text));
		txtProduct.setTextColor(getResources().getColor(R.color.dark_text));
//		BizApp.getInstance().setTab(PersistentKeyUtil.TAB_ACTION);
		PrefsUtils.setSubTab(PersistentKeyUtil.TAB_ACTION);
		tabHost.setCurrentTab(4);
	}

	private void selectTab6() {
		PrefsUtils.setIsNoti(null);
		tab1.setBackgroundResource(0);
		tab2.setBackgroundResource(0);
		tab3.setBackgroundResource(0);
		tab4.setBackgroundResource(0);
		tab5.setBackgroundResource(0);
		tab6.setBackgroundResource(R.drawable.tab_bg_press);
		tabTask.setBackgroundResource(R.drawable.tab_task);
		tabReport.setBackgroundResource(R.drawable.tab_report);
		tabClient.setBackgroundResource(R.drawable.tab_client);
		tabMessage.setBackgroundResource(R.drawable.tab_message);
		tabAction.setBackgroundResource(R.drawable.tab_dynamic_press);
		tabProduct.setBackgroundResource(R.drawable.tab_product);
		txtTask.setTextColor(getResources().getColor(R.color.dark_text));
		txtReport.setTextColor(getResources().getColor(R.color.dark_text));
		txtClient.setTextColor(getResources().getColor(R.color.dark_text));
		txtMsg.setTextColor(getResources().getColor(R.color.dark_text));
		txtAction.setTextColor(getResources().getColor(R.color.deap_blue_text));
		txtProduct.setTextColor(getResources().getColor(R.color.dark_text));
//		BizApp.getInstance().setTab(PersistentKeyUtil.TAB_PRODUCT);
		PrefsUtils.setSubTab(PersistentKeyUtil.TAB_PRODUCT);
		tabHost.setCurrentTab(5);
	}

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
	}

	@Override
	protected void onResume() {
		super.onResume();
		selectTab();
	}

	@Override
	protected void onPause() {
		super.onPause();
	}

}